/*
 * @(#)MtmWfStepFormRecord.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.MtmWfStepFormRecord;
import com.dcivision.workflow.bean.WorkflowStep;

/**
 * MtmWfStepFormRecordDAObject.java
 *
 * This class is the data access bean for table "MTM_WF_STEP_FORM_RECORD".
 *
 * @author      Tony Chen
 * @company     DCIVision Limited
 * @creation date   14/04/2005
 * @version     $Revision: 1.8 $
*/

public class MtmWfStepFormRecordDAObject extends AbstractDAObject {
	
  public static final String REVISION = "$Revision: 1.8 $";

  public static final String TABLE_NAME = "MTM_WF_STEP_FORM_RECORD";

  public MtmWfStepFormRecordDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("WORKFLOW_STEP_ID");
    this.vecDBColumn.add("FORM_RECORD_ID");
    this.vecDBColumn.add("FORM_PAGE_ID");
    this.vecDBColumn.add("FORM_SECTION_ID");
    this.vecDBColumn.add("FORM_ELEMENT_ID");
    this.vecDBColumn.add("PERMISSION");
    this.vecDBColumn.add("MANDATORY");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.FORM_RECORD_ID, A.FORM_PAGE_ID, A.FORM_SECTION_ID, A.FORM_ELEMENT_ID, A.PERMISSION, A.MANDATORY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmWfStepFormRecord tmpMtmWfStepFormRecord = new MtmWfStepFormRecord();
          tmpMtmWfStepFormRecord.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWfStepFormRecord.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpMtmWfStepFormRecord.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpMtmWfStepFormRecord.setFormPageID(getResultSetInteger(rs, "FORM_PAGE_ID"));
          tmpMtmWfStepFormRecord.setFormSectionID(getResultSetInteger(rs, "FORM_SECTION_ID"));
          tmpMtmWfStepFormRecord.setFormElementID(getResultSetInteger(rs, "FORM_ELEMENT_ID"));
          tmpMtmWfStepFormRecord.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWfStepFormRecord.setMandatory(getResultSetString(rs, "MANDATORY"));
          tmpMtmWfStepFormRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWfStepFormRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWfStepFormRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWfStepFormRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWfStepFormRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWfStepFormRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWfStepFormRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getCreatorID()));
          tmpMtmWfStepFormRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getUpdaterID()));
          return(tmpMtmWfStepFormRecord);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.FORM_RECORD_ID, A.FORM_PAGE_ID, A.FORM_SECTION_ID, A.FORM_ELEMENT_ID, A.PERMISSION, A.MANDATORY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          MtmWfStepFormRecord tmpMtmWfStepFormRecord = new MtmWfStepFormRecord();
          tmpMtmWfStepFormRecord.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWfStepFormRecord.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpMtmWfStepFormRecord.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpMtmWfStepFormRecord.setFormPageID(getResultSetInteger(rs, "FORM_PAGE_ID"));
          tmpMtmWfStepFormRecord.setFormSectionID(getResultSetInteger(rs, "FORM_SECTION_ID"));
          tmpMtmWfStepFormRecord.setFormElementID(getResultSetInteger(rs, "FORM_ELEMENT_ID"));
          tmpMtmWfStepFormRecord.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWfStepFormRecord.setMandatory(getResultSetString(rs, "MANDATORY"));
          tmpMtmWfStepFormRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWfStepFormRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWfStepFormRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWfStepFormRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWfStepFormRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWfStepFormRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWfStepFormRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getCreatorID()));
          tmpMtmWfStepFormRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getUpdaterID()));
          tmpMtmWfStepFormRecord.setRecordCount(totalNumOfRecord);
          tmpMtmWfStepFormRecord.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpMtmWfStepFormRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.FORM_RECORD_ID, A.FORM_PAGE_ID, A.FORM_SECTION_ID, A.FORM_ELEMENT_ID, A.PERMISSION, A.MANDATORY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWfStepFormRecord tmpMtmWfStepFormRecord = new MtmWfStepFormRecord();
          tmpMtmWfStepFormRecord.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWfStepFormRecord.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpMtmWfStepFormRecord.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpMtmWfStepFormRecord.setFormPageID(getResultSetInteger(rs, "FORM_PAGE_ID"));
          tmpMtmWfStepFormRecord.setFormSectionID(getResultSetInteger(rs, "FORM_SECTION_ID"));
          tmpMtmWfStepFormRecord.setFormElementID(getResultSetInteger(rs, "FORM_ELEMENT_ID"));
          tmpMtmWfStepFormRecord.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWfStepFormRecord.setMandatory(getResultSetString(rs, "MANDATORY"));
          tmpMtmWfStepFormRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWfStepFormRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWfStepFormRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWfStepFormRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWfStepFormRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWfStepFormRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWfStepFormRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getCreatorID()));
          tmpMtmWfStepFormRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getUpdaterID()));
          result.add(tmpMtmWfStepFormRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWfStepFormRecord tmpMtmWfStepFormRecord = (MtmWfStepFormRecord)((MtmWfStepFormRecord)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   MTM_WF_STEP_FORM_RECORD(ID, WORKFLOW_STEP_ID, FORM_RECORD_ID, FORM_PAGE_ID, FORM_SECTION_ID, FORM_ELEMENT_ID, PERMISSION, MANDATORY, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpMtmWfStepFormRecord.getWorkflowStepID());
        setPrepareStatement(preStat, 3, tmpMtmWfStepFormRecord.getFormRecordID());
        setPrepareStatement(preStat, 4, tmpMtmWfStepFormRecord.getFormPageID());
        setPrepareStatement(preStat, 5, tmpMtmWfStepFormRecord.getFormSectionID());
        setPrepareStatement(preStat, 6, tmpMtmWfStepFormRecord.getFormElementID());
        setPrepareStatement(preStat, 7, tmpMtmWfStepFormRecord.getPermission());
        setPrepareStatement(preStat, 8, tmpMtmWfStepFormRecord.getMandatory());
        setPrepareStatement(preStat, 9, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 10, new Integer(0));
        setPrepareStatement(preStat, 11, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 12, currTime);
        setPrepareStatement(preStat, 13, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 14, currTime);
        preStat.executeUpdate();
        tmpMtmWfStepFormRecord.setID(nextID);
        tmpMtmWfStepFormRecord.setCreatorID(sessionContainer.getUserRecordID());
        tmpMtmWfStepFormRecord.setCreateDate(currTime);
        tmpMtmWfStepFormRecord.setUpdaterID(sessionContainer.getUserRecordID());
        tmpMtmWfStepFormRecord.setUpdateDate(currTime);
        tmpMtmWfStepFormRecord.setUpdateCount(new Integer(0));
        tmpMtmWfStepFormRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getCreatorID()));
        tmpMtmWfStepFormRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getUpdaterID()));
        return(tmpMtmWfStepFormRecord);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWfStepFormRecord tmpMtmWfStepFormRecord = (MtmWfStepFormRecord)((MtmWfStepFormRecord)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE MTM_WF_STEP_FORM_RECORD ");
        sqlStat.append("SET  WORKFLOW_STEP_ID=?, FORM_RECORD_ID=?, FORM_PAGE_ID=?, FORM_SECTION_ID=?, FORM_ELEMENT_ID=?, PERMISSION=?, MANDATORY=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmWfStepFormRecord.getWorkflowStepID());
        setPrepareStatement(preStat, 2, tmpMtmWfStepFormRecord.getFormRecordID());
        setPrepareStatement(preStat, 3, tmpMtmWfStepFormRecord.getFormPageID());
        setPrepareStatement(preStat, 4, tmpMtmWfStepFormRecord.getFormSectionID());
        setPrepareStatement(preStat, 5, tmpMtmWfStepFormRecord.getFormElementID());
        setPrepareStatement(preStat, 6, tmpMtmWfStepFormRecord.getPermission());
        setPrepareStatement(preStat, 7, tmpMtmWfStepFormRecord.getMandatory());
        setPrepareStatement(preStat, 8, new Integer(tmpMtmWfStepFormRecord.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 9, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 10, currTime);
        setPrepareStatement(preStat, 11, tmpMtmWfStepFormRecord.getID());
        setPrepareStatement(preStat, 12, tmpMtmWfStepFormRecord.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpMtmWfStepFormRecord.setUpdaterID(sessionContainer.getUserRecordID());
          tmpMtmWfStepFormRecord.setUpdateDate(currTime);
          tmpMtmWfStepFormRecord.setUpdateCount(new Integer(tmpMtmWfStepFormRecord.getUpdateCount().intValue() + 1));
          tmpMtmWfStepFormRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getCreatorID()));
          tmpMtmWfStepFormRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getUpdaterID()));
          return(tmpMtmWfStepFormRecord);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWfStepFormRecord tmpMtmWfStepFormRecord = (MtmWfStepFormRecord)((MtmWfStepFormRecord)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmWfStepFormRecord.getID());
        setPrepareStatement(preStat, 2, tmpMtmWfStepFormRecord.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpMtmWfStepFormRecord);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    MtmWfStepFormRecord tmpMtmWfStepFormRecord = (MtmWfStepFormRecord)this.oldValue;

    if (tmpMtmWfStepFormRecord != null) {
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getID()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getWorkflowStepID()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getFormRecordID()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getFormPageID()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getFormSectionID()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getFormElementID()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getPermission()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getMandatory()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getUpdateDate()));
    }

    tmpMtmWfStepFormRecord = (MtmWfStepFormRecord)obj;
    if (tmpMtmWfStepFormRecord != null) {
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getID()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getWorkflowStepID()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getFormRecordID()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getFormPageID()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getFormSectionID()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getFormElementID()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getPermission()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getMandatory()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpMtmWfStepFormRecord.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  
  /**
   * delete object list by workflow step
   * 
   * @param workflowStepID The workflow step ID
   * @throws ApplicationException
   */
  public synchronized void deleteByWorkflowStepID(Integer workflowStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    
    synchronized(dbConn) {
      try {
        
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE MTM_WF_STEP_FORM_RECORD ");
        sqlStat.append("SET  RECORD_STATUS=? ");
        sqlStat.append("WHERE  WORKFLOW_STEP_ID=? AND RECORD_STATUS=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_INACTIVE);
        setPrepareStatement(preStat, 2, workflowStepID);
        setPrepareStatement(preStat, 3, GlobalConstant.RECORD_STATUS_ACTIVE);
        
        preStat.executeUpdate();

      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Gets a list for given the workflow step
   * 
   * @param workflowStepID The workflow step ID 
   * @return list of MtmWfStepFormRecord object
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWfStepFormRecord
   */
  public synchronized List getListByWorkflowStepID(Integer workflowStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.FORM_RECORD_ID, A.FORM_PAGE_ID, A.FORM_SECTION_ID, A.FORM_ELEMENT_ID, A.PERMISSION, A.MANDATORY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowStepID);
        
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWfStepFormRecord tmpMtmWfStepFormRecord = new MtmWfStepFormRecord();
          tmpMtmWfStepFormRecord.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWfStepFormRecord.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpMtmWfStepFormRecord.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpMtmWfStepFormRecord.setFormPageID(getResultSetInteger(rs, "FORM_PAGE_ID"));
          tmpMtmWfStepFormRecord.setFormSectionID(getResultSetInteger(rs, "FORM_SECTION_ID"));
          tmpMtmWfStepFormRecord.setFormElementID(getResultSetInteger(rs, "FORM_ELEMENT_ID"));
          tmpMtmWfStepFormRecord.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWfStepFormRecord.setMandatory(getResultSetString(rs, "MANDATORY"));
          tmpMtmWfStepFormRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWfStepFormRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWfStepFormRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWfStepFormRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWfStepFormRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWfStepFormRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWfStepFormRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getCreatorID()));
          tmpMtmWfStepFormRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getUpdaterID()));
          result.add(tmpMtmWfStepFormRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Gets a list for given workflow step,form record and permission
   * 
   * @param workflowStepID The workflow step ID
   * @param formRecordID The form record ID
   * @param permission
   * @return list of MtmWfStepFormRecord object
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWfStepFormRecord
   * @see com.dcivision.workflow.bean.WorkflowStep
   * @see com.dcivision.form.bean.FormRecord
   */
  public synchronized List getListByWorkflowStepIDFormRecordIDPermission(
  		Integer workflowStepID, 
			Integer formRecordID, 
			String permission) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.FORM_RECORD_ID, A.FORM_PAGE_ID, A.FORM_SECTION_ID, A.FORM_ELEMENT_ID, A.PERMISSION, A.MANDATORY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? AND A.FORM_RECORD_ID = ? AND A.PERMISSION = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowStepID);
        this.setPrepareStatement(preStat, 3, formRecordID);
        this.setPrepareStatement(preStat, 4, permission);
        
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWfStepFormRecord tmpMtmWfStepFormRecord = new MtmWfStepFormRecord();
          tmpMtmWfStepFormRecord.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWfStepFormRecord.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpMtmWfStepFormRecord.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpMtmWfStepFormRecord.setFormPageID(getResultSetInteger(rs, "FORM_PAGE_ID"));
          tmpMtmWfStepFormRecord.setFormSectionID(getResultSetInteger(rs, "FORM_SECTION_ID"));
          tmpMtmWfStepFormRecord.setFormElementID(getResultSetInteger(rs, "FORM_ELEMENT_ID"));
          tmpMtmWfStepFormRecord.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWfStepFormRecord.setMandatory(getResultSetString(rs, "MANDATORY"));
          tmpMtmWfStepFormRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWfStepFormRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWfStepFormRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWfStepFormRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWfStepFormRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWfStepFormRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWfStepFormRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getCreatorID()));
          tmpMtmWfStepFormRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getUpdaterID()));
          result.add(tmpMtmWfStepFormRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Gets a operate list for given worklow step and form record
   * 
   * @param workflowStepID The workflow step ID
   * @param formRecordID The form record ID
   * @return list of MtmWfStepFormRecord object
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWfStepFormRecord
   * @see com.dcivision.workflow.bean.WorkflowStep
   * @see com.dcivision.form.bean.FormRecord
   */
  public synchronized List getOperateListByWorkflowStepIDFormRecordID(
        Integer workflowStepID, 
				Integer formRecordID ) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.FORM_RECORD_ID, A.FORM_PAGE_ID, A.FORM_SECTION_ID, A.FORM_ELEMENT_ID, A.PERMISSION, A.MANDATORY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? AND A.FORM_RECORD_ID = ? AND A.PERMISSION <> ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowStepID);
        this.setPrepareStatement(preStat, 3, formRecordID);
        this.setPrepareStatement(preStat, 4, WorkflowStep.FORM_PROCESS_PERMISSION_SUBMIT );
        
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWfStepFormRecord tmpMtmWfStepFormRecord = new MtmWfStepFormRecord();
          tmpMtmWfStepFormRecord.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWfStepFormRecord.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpMtmWfStepFormRecord.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpMtmWfStepFormRecord.setFormPageID(getResultSetInteger(rs, "FORM_PAGE_ID"));
          tmpMtmWfStepFormRecord.setFormSectionID(getResultSetInteger(rs, "FORM_SECTION_ID"));
          tmpMtmWfStepFormRecord.setFormElementID(getResultSetInteger(rs, "FORM_ELEMENT_ID"));
          tmpMtmWfStepFormRecord.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWfStepFormRecord.setMandatory(getResultSetString(rs, "MANDATORY"));
          tmpMtmWfStepFormRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWfStepFormRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWfStepFormRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWfStepFormRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWfStepFormRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWfStepFormRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWfStepFormRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getCreatorID()));
          tmpMtmWfStepFormRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getUpdaterID()));
          result.add(tmpMtmWfStepFormRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a string of form record permission by workflow step and form record for given worklow step and form record
   * 
   * @param workflowStepID The workflow step ID
   * @param formRecordID The form record ID
   * @return string of the permission
   * @throws ApplicationException
   */
  public synchronized String getFormRecordPermissionByWorkflowStepIDFormRecordID(Integer workflowStepID, Integer formRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    String resultPermission = "";

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.FORM_RECORD_ID, A.FORM_PAGE_ID, A.FORM_SECTION_ID, A.FORM_ELEMENT_ID, A.PERMISSION, A.MANDATORY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? ");
        sqlStat.append("AND A.FORM_RECORD_ID = ? ");
        sqlStat.append("AND A.PERMISSION <> ? ");
        sqlStat.append("AND A.FORM_PAGE_ID IS NULL AND A.FORM_SECTION_ID IS NULL AND A.FORM_ELEMENT_ID IS NULL ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowStepID);
        this.setPrepareStatement(preStat, 3, formRecordID);        
        this.setPrepareStatement(preStat, 4, WorkflowStep.FORM_PROCESS_PERMISSION_SUBMIT);
        
        rs = preStat.executeQuery();
        if (rs.next()) {
          resultPermission = getResultSetString(rs, "PERMISSION");                 
        }
        
        return(resultPermission);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a string of form page permission by workflow step , form record and form page for given worklow step , form record and form page
   * 
   * @param workflowStepID The workflow step ID
   * @param formRecordID The form record ID
   * @param formPageID The form page ID
   * @return string of the permission
   * @throws ApplicationException
   */
  public synchronized String getFormPagePermissionByWorkflowStepIDFormRecordIDFormPageID(Integer workflowStepID, Integer formRecordID, Integer formPageID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    String resultPermission = "";

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.FORM_RECORD_ID, A.FORM_PAGE_ID, A.FORM_SECTION_ID, A.FORM_ELEMENT_ID, A.PERMISSION, A.MANDATORY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? ");
        sqlStat.append("AND A.FORM_RECORD_ID =? AND A.FORM_PAGE_ID =? ");
        sqlStat.append("AND A.FORM_SECTION_ID IS NULL AND A.FORM_ELEMENT_ID IS NULL ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowStepID);
        this.setPrepareStatement(preStat, 3, formRecordID);
        this.setPrepareStatement(preStat, 4, formPageID);
 
        
        rs = preStat.executeQuery();
        if (rs.next()) {
            resultPermission = getResultSetString(rs, "PERMISSION") ; 
        }
        
        return(resultPermission);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a string of form section permission by workflow step,form record,form page and form section
   * for given workflow step,,form record,form page and form section
   * 
   * @param workflowStepID The workflow step ID
   * @param formRecordID The form record ID
   * @param formPageID The form page ID
   * @param formSectionID The form section ID
   * @return string of form section permission
   * @throws ApplicationException
   */
  public synchronized String getFormSectionPermissionByWorkflowStepIDFormRecordIDFormPageIDFormSectionID(Integer workflowStepID, Integer formRecordID, Integer formPageID, Integer formSectionID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    String resultPermission = "";

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.FORM_RECORD_ID, A.FORM_PAGE_ID, A.FORM_SECTION_ID, A.FORM_ELEMENT_ID, A.PERMISSION, A.MANDATORY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? ");
        sqlStat.append("AND A.FORM_RECORD_ID =? AND A.FORM_PAGE_ID =? ");
        sqlStat.append("AND A.FORM_SECTION_ID =? ");
        sqlStat.append("AND A.FORM_ELEMENT_ID IS NULL ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowStepID);
        this.setPrepareStatement(preStat, 3, formRecordID);
        this.setPrepareStatement(preStat, 4, formPageID);
        this.setPrepareStatement(preStat, 5, formSectionID);
        
        rs = preStat.executeQuery();
        if (rs.next()) {
            resultPermission = getResultSetString(rs, "PERMISSION");
        }
        
        return(resultPermission);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a string of form element permission by workflow step,form record,form page,form section and form element
   * for given workflow step,form record,form page,form section and form element
   * 
   * @param workflowStepID The workflow step ID
   * @param formRecordID The form record ID
   * @param formPageID The form page ID
   * @param formSectionID The form section ID
   * @param formElementID The form element ID
   * @return string of form element permission
   * @throws ApplicationException
   */
  public synchronized String getFormElementPermissionByWorkflowStepIDFormRecordIDFormPageIDFormSectionIDFormElementID(Integer workflowStepID, Integer formRecordID, Integer formPageID, Integer formSectionID, Integer formElementID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    String resultPermission = "";

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.FORM_RECORD_ID, A.FORM_PAGE_ID, A.FORM_SECTION_ID, A.FORM_ELEMENT_ID, A.PERMISSION, A.MANDATORY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? ");
        sqlStat.append("AND A.FORM_RECORD_ID =? AND A.FORM_PAGE_ID =? ");
        sqlStat.append("AND A.FORM_SECTION_ID =? AND A.FORM_ELEMENT_ID =? ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowStepID);
        this.setPrepareStatement(preStat, 3, formRecordID);
        this.setPrepareStatement(preStat, 4, formPageID);
        this.setPrepareStatement(preStat, 5, formSectionID);
        this.setPrepareStatement(preStat, 6, formElementID);
        
        rs = preStat.executeQuery();
        if (rs.next()) {
            resultPermission = getResultSetString(rs, "PERMISSION") ; 
        }else{
        	resultPermission = WorkflowStep.FORM_PROCESS_PERMISSION_CHANGE;
        }
        
        return(resultPermission);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Chech the whether the form is mandatory sumited in the first step of the workflow
   *  
   * @param formRecordID The form record ID
   * @param workflowRecordID The workflow record ID
   * @return boolean
   * @throws ApplicationException
   */
  public synchronized boolean getByFormRecordIDWorkflowRecordID(Integer formRecordID, Integer workflowRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append(" SELECT  DISTINCT A.FORM_RECORD_ID ");        
        sqlStat.append(" FROM MTM_WF_STEP_FORM_RECORD A, WORKFLOW_STEP B ");        
        sqlStat.append(" WHERE A.WORKFLOW_STEP_ID = B.ID ");
        sqlStat.append(" AND A.RECORD_STATUS = B.RECORD_STATUS ");
        sqlStat.append(" AND A.RECORD_STATUS = ? ");
        sqlStat.append(" AND B.STEP_SEQ = ? ");             
        sqlStat.append(" AND A.MANDATORY = ? ");
        sqlStat.append(" AND A.FORM_RECORD_ID = ? ");   
        sqlStat.append(" AND B.WORKFLOW_RECORD_ID = ? ");   
        
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, new Integer(1));
        this.setPrepareStatement(preStat, 3, GlobalConstant.TRUE);
        this.setPrepareStatement(preStat, 4, formRecordID);
        this.setPrepareStatement(preStat, 5, workflowRecordID);
        rs = preStat.executeQuery();
        if (rs.next()) {
          return true;
        } else {
          return false;
        }
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * Check if has permission by workflow step ID and form Record ID.
   * 
   * @param workflowStepID The workflow step ID
   * @param formRecordID The form record ID
   * @return boolean
   * @throws ApplicationException
   */
  public synchronized boolean hasPermissionByWorkflowStepIDFormRecordID(Integer workflowStepID, Integer formRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    String resultPermission = "";

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.FORM_RECORD_ID, A.FORM_PAGE_ID, A.FORM_SECTION_ID, A.FORM_ELEMENT_ID, A.PERMISSION, A.MANDATORY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_STEP_ID = ? ");
        sqlStat.append("AND A.FORM_RECORD_ID = ? ");
        sqlStat.append("AND A.PERMISSION <> ? ");
        
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowStepID);
        this.setPrepareStatement(preStat, 3, formRecordID);        
        this.setPrepareStatement(preStat, 4, WorkflowStep.FORM_PROCESS_PERMISSION_SUBMIT);
        
        rs = preStat.executeQuery();
        if (rs.next()) {
          return true;                 
        }
        
        return(false);      
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * get list first step binding with the form element by workflow record ID and form record ID
   * @param workflowStepID
   * @param formRecordID
   * @return List
   * @throws ApplicationException
   */
  public synchronized List getListByWorkflowRecordIDFormRecordID(
      Integer workflowRecordID, 
      Integer formRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_STEP_ID, A.FORM_RECORD_ID, A.FORM_PAGE_ID, A.FORM_SECTION_ID, A.FORM_ELEMENT_ID, A.PERMISSION, A.MANDATORY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_FORM_RECORD A, WORKFLOW_STEP B ");
        sqlStat.append("WHERE  A.WORKFLOW_STEP_ID = B.ID AND A.RECORD_STATUS = ? AND B.WORKFLOW_RECORD_ID = ? AND A.FORM_RECORD_ID = ? AND B.STEP_SEQ = 1 ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowRecordID);
        this.setPrepareStatement(preStat, 3, formRecordID);
        
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWfStepFormRecord tmpMtmWfStepFormRecord = new MtmWfStepFormRecord();
          tmpMtmWfStepFormRecord.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWfStepFormRecord.setWorkflowStepID(getResultSetInteger(rs, "WORKFLOW_STEP_ID"));
          tmpMtmWfStepFormRecord.setFormRecordID(getResultSetInteger(rs, "FORM_RECORD_ID"));
          tmpMtmWfStepFormRecord.setFormPageID(getResultSetInteger(rs, "FORM_PAGE_ID"));
          tmpMtmWfStepFormRecord.setFormSectionID(getResultSetInteger(rs, "FORM_SECTION_ID"));
          tmpMtmWfStepFormRecord.setFormElementID(getResultSetInteger(rs, "FORM_ELEMENT_ID"));
          tmpMtmWfStepFormRecord.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWfStepFormRecord.setMandatory(getResultSetString(rs, "MANDATORY"));
          tmpMtmWfStepFormRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWfStepFormRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWfStepFormRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWfStepFormRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWfStepFormRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWfStepFormRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWfStepFormRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getCreatorID()));
          tmpMtmWfStepFormRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWfStepFormRecord.getUpdaterID()));
          result.add(tmpMtmWfStepFormRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
}
